/**
 * 
 */
package za.co.sindi.oauth.core.scanner.impl;

import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;

import za.co.sindi.oauth.core.scanner.ClassScanner;
import za.co.sindi.oauth.core.scanner.filter.TypeFilter;
import za.co.sindi.oauth.exception.ScanningException;

/**
 * @author Buhake Sindi
 * @since 09 April 2012
 *
 */
public class CombinedClassScanner implements ClassScanner {

	private ClassScanner[] scanners;
	
	/**
	 * @param scanners
	 */
	public CombinedClassScanner(ClassScanner... scanners) {
		super();
		
		if (scanners == null || scanners.length == 0) {
			throw new IllegalArgumentException("This class requires one or more ClassScanner.");
		}
		
		this.scanners = scanners;
	}

	/* (non-Javadoc)
	 * @see com.neurologic.oauth.core.scanner.ClassScanner#addIncludeFilter(com.neurologic.oauth.core.scanner.filter.TypeFilter)
	 */
	@Override
	public void addIncludeFilter(TypeFilter filter) {
		// TODO Auto-generated method stub
		if (scanners != null) {
			for (ClassScanner scanner : scanners) {
				scanner.addIncludeFilter(filter);
			}
		}
	}

	/* (non-Javadoc)
	 * @see com.neurologic.oauth.core.scanner.ClassScanner#addExcludeFilter(com.neurologic.oauth.core.scanner.filter.TypeFilter)
	 */
	@Override
	public void addExcludeFilter(TypeFilter filter) {
		// TODO Auto-generated method stub
		if (scanners != null) {
			for (ClassScanner scanner : scanners) {
				scanner.addExcludeFilter(filter);
			}
		}
	}

	/* (non-Javadoc)
	 * @see com.neurologic.oauth.core.scanner.ClassScanner#scan(java.lang.String)
	 */
	@Override
	public Set<Class<?>> scan(String basePackageName) throws ScanningException {
		// TODO Auto-generated method stub
		Set<Class<?>> classes = new LinkedHashSet<Class<?>>();
		if (scanners != null) {
			for (ClassScanner scanner : scanners) {
				classes.addAll(scanner.scan(basePackageName));
			}
		}
		
		return Collections.unmodifiableSet(classes);
	}
}
